home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / tlp4v11c / source / inptest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-23  |  3.3 KB  |  110 lines

  1. #include <dos.h>    /* for inportb() */
  2. #include <stdio.h>    /* for standard i/o */
  3.  
  4. #include "uniinp.h"    /* tgd keyboard and joystick functions */
  5.  
  6. #define INP_ST1 0x03da    /* VGA port to query vertical retrace */
  7.  
  8. #define UNIKEY_ESC 1    /* uniinp keycode of ESC key */
  9. #define UNIKEY_J 36    /* uniinp keycode of j key */
  10. #define UNIKEY_K 37     /* uniinp keycode of k key */
  11.  
  12. void keyfind(void)
  13. { /* key states */
  14.   const char keyreleased[]="RELEASED",keypressed[]="PRESSED ";
  15.   const char *keyaction[2];
  16.   /* last used key */
  17.   ubyte key;
  18.  
  19.   /* init keystates */
  20.   keyaction[0]=keyreleased; keyaction[1]=keypressed;
  21.  
  22.   printf("\n\n>>> uniinp keycode find selected <<<\n"
  23.      "now press the key(s) of which you want to know the\n"
  24.      "uniinp key code returned by uniinp's lastkey() function.\n"
  25.      "press 'ESC' (uniinp key code 01) to abort.\n\n");
  26.  
  27.   /* break on ESC */
  28.   while ((key=lastkey())!=UNIKEY_ESC)
  29.   { if (key) printf("last uniinp key code:  %3u   key status: %s\r",key,keyaction[keystatus(key)&1]);
  30.   };
  31. }
  32.  
  33. /* this procedure waits for the end of a vertical retrace */
  34. void waitvret(void)
  35. {
  36.   while (!(inportb(INP_ST1)&8));
  37.   while (inportb(INP_ST1)&8);
  38. }
  39.  
  40. void joytest(void)
  41. { /* bitfield (indicates connected joysticks) */
  42.   ubyte stickconnect=0;
  43.   /* joystick positions */
  44.   joypos *sticks;
  45.  
  46.   /* wait until user has released 'j' key */
  47.   while (!lastkey());
  48.  
  49.   printf("\n\n>>> uniinp joystick test selected <<<\n"
  50.      "determining which joysticks are connected...\n");
  51.  
  52.   /* check if joystick #0 is connected */
  53.   printf("joystick #0: ");
  54.   /* it is not allowed to call readstick() during a vertical retrace
  55.    * so wait for the end of one.
  56.    */
  57.   waitvret();
  58.   if (readstick(1)) { printf("ok.\n"); stickconnect=1; }
  59.   else { printf("not connected.\n"); };
  60.  
  61.   /* check if joystick #1 is connected */
  62.   printf("joystick #1: ");
  63.   /* it is not allowed to call readstick() during a vertical retrace
  64.    * so wait for the end of one.
  65.    */
  66.   waitvret();
  67.   if (readstick(2)) { printf("ok.\n"); stickconnect|=2; }
  68.   else { printf("not connected.\n"); };
  69.  
  70.   if (!stickconnect) { printf("\nno joysticks connected."); return; };
  71.  
  72.   printf("\nnow querying joystick(s). press ANY key to abort.\n");
  73.   while (!lastkey())
  74.   { waitvret();
  75.     if (stickconnect&1) { sticks=readstick(1); printf("x0:%3u  y0:%3u    ",sticks->joy0xpos,sticks->joy0ypos); };
  76.     if (stickconnect&2) { sticks=readstick(2); printf("x1:%3u  y1:%3u    ",sticks->joy1xpos,sticks->joy1ypos); };
  77.     printf("buttons:%3u",readjbutton()&0xf0);
  78.     printf("\r");
  79.   };
  80. }
  81.  
  82. main()
  83. { /* last used key */
  84.   ubyte key;
  85.  
  86.   printf("uniinp key code find / joystick test V1.0a\n"
  87.      "==========================================\n\n"
  88.      "please select input device to test: k)eyboard\n"
  89.      "                                    j)oystick\n"
  90.      "anything else aborts.");
  91.  
  92.   /* force keyboard to generate 'native' key scan codes */
  93.   keyboardled(0);
  94.   /* install uniinp keyboard handler */
  95.   getkeyboard();
  96.  
  97.   /* wait for a selection */
  98.   while (!(key=lastkey()));
  99.   if (key==UNIKEY_K) keyfind();
  100.   if (key==UNIKEY_J) joytest();
  101.  
  102.   /* re-install BIOS keyboard handler */
  103.   freekeyboard();
  104.   /* restore old BIOS keyboard LED state */
  105.   keyboardled(sysled());
  106.  
  107.   printf("\n");
  108.   return(0);
  109. }
  110.